package com.sfpay.coreplatform.order.service.composite.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.common.enums.TradeType;
import com.sfpay.coreplatform.order.common.enums.tradeStatus;
import com.sfpay.coreplatform.order.service.composite.IHandler;
import com.sfpay.coreplatform.order.service.composite.QueryOrderService;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;
import com.sfpay.coreplatform.order.valueobject.tmo.PayOrder;
import com.sfpay.coreplatform.order.valueobject.tmo.TradeOrder;

@Service
public class CheckReverseHandler extends AbstractHandler {

	public Response Handler(PayRequest request, PayContext payContext)
			throws OrderServiceException {
		Response response = new Response();
		List<TradeOrder> trades = queryOrderService.queryPayTradeByOutTradeNo(
				request.getPlatformMemberNo(), request.getOutTradeNo());

		if (trades == null || trades.size() == 0) {
			response.setReturnCode(OrderResponseCode.ORDER_PAYORDER_ISNOT_EXIST);
			response.setReturnMsg("method:reversePay businessNo:"
					+ request.getOutTradeNo() + " is not exist");
			return response;
		}

		int size = trades.size();
		if (size == 1) {// 只存在一条记录

			TradeOrder trade = trades.get(0);
			if (TradeType.PAY == trade.getTradeType()
					&& tradeStatus.SUCCESS == trade.getOrderStatus()) {// 支付类型交易成功
				// 冲正
				createPayRequest(request, request.getPlatformMemberNo(),
						request.getOutTradeNo(), trade);
				request.setPayAmt(-request.getPayAmt());// 把金额设为负
			} else {
				response.setReturnCode(OrderResponseCode.ORDER_OTHER_EXCEPTION);
				response.setReturnMsg("method:reversePay businessNo:"
						+ request.getOutTradeNo()
						+ " status is failure,not reverse");
				return response;
			}

		}

		// 已经存在冲正
		if (trades.size() > 1) {
			for (TradeOrder trade : trades) {

				if (TradeType.REVERSE == trade.getTradeType()) {

					if (tradeStatus.FAILURE == trade.getOrderStatus()) {
						// 冲正
						tradeStatus status = getTradeStatus(trades,
								TradeType.CANCEL);
						if (status == null || status == tradeStatus.FAILURE) {

							request = createPayRequest(request,
									request.getPlatformMemberNo(),
									request.getOutTradeNo(), trade);
							payContext.setTradeNo(trade.getOrderNo());// 得到已经存在交易订单号
						} else {
							response.setReturnCode(OrderResponseCode.ORDER_ALREADY_CANCEL);
							response.setReturnMsg("method:reversePay businessNo:"
									+ request.getOutTradeNo()
									+ " already cancel success,not reverse");
							return response;
						}
					} else {
						response.setReturnCode(OrderResponseCode.ORDER_ALREADY_REVERSE);
						response.setReturnMsg("method:reversePay businessNo:"
								+ request.getOutTradeNo()
								+ " already reverse success");
						return response;
					}
					break;
				} else if (TradeType.CANCEL == trade.getTradeType()
						&& tradeStatus.SUCCESS == trade.getOrderStatus()) {
					response.setReturnCode(OrderResponseCode.ORDER_ALREADY_CANCEL);
					response.setReturnMsg("method:reversePay businessNo:"
							+ request.getOutTradeNo()
							+ " already cancel success,not reverse");
					return response;
				}
			}

		}
		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);
		return response;
	}

	/**
	 * 创建PayRequest
	 * 
	 * @param platformMemberNo
	 * @param outTradeNo
	 * @param tradeOrder
	 * @return
	 */
	private PayRequest createPayRequest(PayRequest request,
			Long platformMemberNo, String outTradeNo, TradeOrder tradeOrder) {
		List<PayOrder> payOrders = queryOrderService
				.queryPayOrderByTradeNo(tradeOrder.getOrderNo());
		if (payOrders == null || payOrders.size() == 0) {

			return null;
		}
		PayOrder payOrder = payOrders.get(0);
		request.setOutTradeNo(outTradeNo);
		request.setPlatformMemberNo(platformMemberNo);
		request.setPayAmt(tradeOrder.getOrderAmt());
		request.setBusinessType(tradeOrder.getBusinessType());
		request.setOrderType(tradeOrder.getOrderType());
		request.setPayeeAcctNo(payOrder.getPayeeAcctNo());
		request.setPayeeMemberNo(payOrder.getPayeeMemberNo());
		request.setPayerAcctNo(payOrder.getPayerAcctNo());
		request.setPayerMemberNo(payOrder.getPayerMemberNo());
		request.setPayPostNo(payOrder.getPayTallyCode());
		request.setTradePostNo(payOrder.getTradeTallyCode());
		request.setTradeInfo(tradeOrder.getDetailInfo());
		request.setSubject(tradeOrder.getSubject());
		request.setAccPasword(false);

		payOrder = null;
		payOrders = null;
		return request;
	}

	@Override
	public String getHandlerName() {
		 
		return "CheckReverseHandler";
	}

}
